package com.example.topguli;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

public class ForkJoinTest extends RecursiveTask<Integer> {
    private Integer start;
    private Integer end;
    private Integer sum=0;
    public  ForkJoinTest(Integer start,Integer end){
        this.start=start;
        this.end=end;
    }
    private Integer edge=10000;

    @Override
    protected Integer compute() {
        if(end<=edge){
            while (end-start!=0){
                sum=sum+start;
                start++;
            }
            return sum;
        }
            ForkJoinTest forkJoinTest = new ForkJoinTest(start, end / 2);
                forkJoinTest.fork();
            ForkJoinTest forkJoinTest1 = new ForkJoinTest(end / 2 +1, end);
            forkJoinTest1.fork();
           return forkJoinTest.join()+forkJoinTest1.join();

    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ForkJoinTest forkJoinTest = new ForkJoinTest(0, 10000);
        ForkJoinPool forkJoinPool = new ForkJoinPool();
//        ForkJoinTask<Integer> submit = forkJoinPool.submit(forkJoinTest);
//        Integer integer = submit.get();
//        System.out.println(integer);
//        forkJoinPool.shutdown();
        forkJoinPool.execute(forkJoinTest);
        System.out.println(forkJoinTest.get());
        forkJoinPool.shutdown();
    }

}
